<template>
	<div class="login">
		<view class="login-img">
			<img style="width: 120px;height: 120px;" src="../static/logo.png" alt="" />
			<view class="login-img-text">
				钢价通
			</view>
		</view>
		<view class="login-agree">
			<label class="radio">
				<radio style="transform:scale(0.8)" @click="isSelect=!isSelect" :checked="isSelect" />
			</label>
			<span class="bot_text">登录表示已阅读同意《<span @click="jumpAgreement" class="agreement">用户协议</span>》</span>
		</view>
		<button v-if="!isSelect" class="login-btn" @click="getUserPhoneNumber">
			一键登录
		</button>
		<button v-else class="login-btn" open-type="getPhoneNumber" @getphonenumber="getUserPhoneNumber">
			一键登录
		</button>
	</div>
	<van-toast id="van-toast" />
</template>

<script>
	// import Toast from '@/wxcomponents/vant/toast/toast'
	// import {
	// 	postVisitPhone
	// } from '@/api/repairApi.js'
	import {
		postVisitPhone,
		getUserExistCode
	} from '@/common/api.js'
	export default {
		data() {
			return {
				isSelect: false,
				authCode:'',
				sessionKey:''
			}
		},
		async mounted(){
		},
		methods: {
			/* 登录 */
			login (){
				// const accountInfo = uni.getAccountInfoSync()
				uni.login({
					// onlyAuthorize: true,
					provider: 'weixin',
					success: function(loginRes) {
						this.authCode = loginRes.code;
						console.log('loginRes', loginRes.code)
					}
				});
			},
			jumpAgreement() {
				uni.navigateTo({
					url: '../agreement/user'
				});
			},
			async getUserPhoneNumber(e) {
				if (!this.isSelect) {
					return uni.showToast({
							title: '请勾选同意协议',
							icon: 'none',
						})
				}
				console.log(e)
				if (e.detail.errMsg == "getPhoneNumber:ok") {
					const encryptedData = e.detail.encryptedData;
					const iv = e.detail.iv;
					// 将encryptedData、iv发送到服务器进行解密获取手机号
					// const phoneNumber = res.data.phoneNumber;
					let params = {
						openId: getApp().globalData.openId,
						encryptedData: encryptedData,
						iv: iv,
						sessionKey: getApp().globalData.sessionKey
					}
					// 此处可以将手机号存储起来，或者进行其他操作
					let resPhone = await postVisitPhone(params)
					console.log('resPhone',resPhone)
					if (resPhone) {
						uni.setStorageSync('openId',getApp().globalData.openId)
						uni.hideLoading({
							complete: () => {
								uni.showToast({
									title: '登录成功',
									icon: 'none',
								})
							}
						})
						uni.reLaunch({
							url:'/pages/market/index'
						})
					} else {
						// Toast('服务器繁忙，请稍后再试')
						uni.showToast({
							title: '服务器繁忙，请稍后再试',
							icon: 'none',
						})
					}
				} else {
					console.log('用户拒绝授权');
					// Toast('用户拒绝授权:'+e.detail.errMsg)
					uni.showToast({
						title: '用户拒绝授权:'+e.detail.errMsg,
						icon: 'none',
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.login {
		width: 100%;
		height: 100%;

		&-img {
			margin: 20px auto 0;
			text-align: center;
			&-text{
				margin-top: -15px;
			}
		}

		&-btn {
			width: 90%;
			margin: 0 auto;
			margin-top: 10px;
			background-color: #4087f2;
			color: #fff;
		}

		&-agree {
			margin-top: 40px;
			height: 40px;
			line-height: 40px;
			text-align: center;
			font-size: 26rpx;

			.bot_text {
				color: #7c7c7c;
			}

			.agreement {
				color: #4087f2;
			}
		}
	}
</style>